/**
 * Basic control test for AtmegaSID [1]
 * 
 *
 * [1]http://roboterclub-freiburg.de/atmega_sound/atmegaSID.html
 * 
 * @author Tom Hlina
 * @version 0.4 (15.11.2010)
 *
 *
 * Copyright (c) 2010 Tom Hlina
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 */


/**
 * Version 0.1  01.11.2010  basic implementation
 * Version 0.2  03.11.2010  added slider window, added c64 font
 * Version 0.3  07.11.2010  add Attack, Decay, Sustain and Release slider
 * Version 0.4  15.11.2010  Fixed font problem
 *
 */

import processing.serial.*;
import controlP5.*;

private static final boolean     gUseC64Font   = false; // install C64Basic.ttf from the data folder befor enable this setting
private static final boolean     gDebug        = false; 
private static final int         gNumberOfRegs = 25;


private static final color       gLabelColor   = 0xff00ff00;
private static final color       gTextColor    = 0xff7C71DA;

private final PFont              gC64Basic20PF = createFont( "C64Basic", 20, false );
private final ControlFont        gC64Basic20   = new ControlFont( gC64Basic20PF );

private ControlP5                controlP5;
private SidInterface             vSid;
private Textarea                 vDebugConsole;

private RegisterCheckbox[]       mRegisters    = new RegisterCheckbox[gNumberOfRegs];

private int[] mOscHB = {  
  1,  1,  1,  1, 1,  1,  1,  1,  1,  1,  2,  2,  2,  2,  2,  2,  2,  3,  3,
  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  6,  6,  6,  7,  7,  8,  8,  9,  9,  10,  10,  11,
  12,  13,  13,  14,  15,  16,  17,  18,  19,  20,  21,  23,  24,  26,  27,  29,  31,  32,
  34,  36,  39,  41,  43,  46,  49,  52,  55,  58,  62,  65,  69,  73,  78,  82,  87,  92,
  98,  104,  110,  117,  124,  131,  139,  147,  156,  165,  175,  185,  196,  208,  220,
  234,  248
};

private int[] mOscLB = {
  22,38,56,75,94,115,137,161,185,212,240,13,44,77,113,150,189,231,19,66,115,
  168,224,27,89,155,226,44,123,206,38,132,231,80,192,54,179,55,196,88,246,157,77,9,207,161,
  128,108,102,111,136,177,236,58,155,18,159,67,1,217,205,223,16,98,216,116,55,36,62,135,2,
  178,154,190,32,197,177,232,111,73,125,15,5,100,53,124,65,139,99,209,222,147,250,30,10
};


void setup() 
{
  size(800,750);
  background(0);
  frameRate(120);


  vSid      = new DummySid();
  controlP5 = new ControlP5(this);
  controlP5.setAutoDraw(false);
  controlP5.setColorLabel( gLabelColor );
  controlP5.setColorValue( gTextColor );

  if( gUseC64Font )
  {
    controlP5.setControlFont( gC64Basic20 );
  }


  controlP5.addButton("Setup", 0, 650, 20, 46, 24);
  controlP5.addButton("Slider", 0, 720, 20, 53, 24);


  vDebugConsole = controlP5.addTextarea("Debug Consolse", "Debug Consolse\n", 600, 50, 180, 680);


  for(int i = 0; i < gNumberOfRegs; i++)
  {
    if( i < 10 )
    {
      mRegisters[i] = new RegisterCheckbox( i, "Register 0" + i + ":", controlP5, 20, 20 + (25*i) );
    }
    else
    {
      mRegisters[i] = new RegisterCheckbox( i, "Register " + i + ":", controlP5, 20, 20 + (25*i) );
    }
  }

  vSid.init();
}

void draw()
{
  background(0);
  writeSid();  //Fixme: timing
  controlP5.draw();
}

void stop()
{
  vSid.stop();
}



private void writeSid()
{
  for( int i = 0; i < gNumberOfRegs; i++ )
  {
    if( mRegisters[i].isDirty() )
    {
      vSid.write(i, mRegisters[i].get());
    }
  }
}



private void updateInfoConsole( String pInfo )
{
  String vTmp;
  vTmp = vDebugConsole.text();

  if( vTmp.length() > 0 )
  {
    vTmp += pInfo + "\n";
    vDebugConsole.setText( vTmp );
  }
}

